/**
 * 
 */
package com.demo2do.strawberry.web.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Page;
import com.demo2do.strawberry.entity.investor.Institution;
import com.demo2do.strawberry.entity.investor.Investor;
import com.demo2do.strawberry.service.InstitutionService;
import com.demo2do.strawberry.service.InvestorService;

/**
 * @author lute
 *
 */
@Controller
@RequestMapping("/institutions")
@MenuSetting("investors")
public class InstitutionController {
	
	@Autowired
	private InstitutionService institutionService;
	
	@Autowired
	private InvestorService investorService;
	
	/**
	 * @return
	 */
	@RequestMapping("")
	public ModelAndView list(Page page, @RequestParam(value = "name", required = false) String name) {
		
		List<Institution> institutions = institutionService.list(page, name);
		
		ModelAndView modelAndView = new ModelAndView("institution/institution-list");
		modelAndView.addObject("institutions", institutions);
		
		List<Investor> latestInvestors = investorService.getLatestInvestors();
		modelAndView.addObject("latestInvestors", latestInvestors);
		
		return modelAndView;
	}
	
	/**
	 * @param id
	 * @return
	 */
	@RequestMapping("/{id}")
	public ModelAndView view(@PathVariable("id") Long id) {
		
		return viewCategory(id, "investors");
	}
	
	/**
	 * @param id
	 * @param category
	 * @return
	 */
	@RequestMapping("/{id}/{category}")
	public ModelAndView viewCategory(@PathVariable("id") Long id, @PathVariable("category") String category) {
		
		ModelAndView modelAndView = new ModelAndView("institution/institution-" + category);
		
		Institution institution = institutionService.load(id);
		modelAndView.addObject("institution", institution);
		
		return modelAndView;
	}
	
}
